package com.circus.trace.controller.material;

import org.nutz.dao.Chain;
import org.nutz.dao.Cnd;
import org.nutz.lang.ContinueLoop;
import org.nutz.lang.Each;
import org.nutz.lang.ExitLoop;
import org.nutz.lang.Lang;
import org.nutz.lang.LoopException;
import org.nutz.plugin.spring.boot.service.entity.PageredData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.circus.trace.bean.material.Coffee;
import com.circus.trace.biz.material.CoffeeService;
import com.circus.trace.biz.material.area.ManorService;
import com.circus.trace.biz.material.area.StorageService;
import com.circus.trace.biz.material.transport.TransportService;
import com.circus.trace.controller.BaseController;
import com.circus.trace.ext.shiro.anno.SINORequiresPermissions;
import com.circus.trace.vo.InstallPermission;

import club.zhcs.common.Result;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@RestController
@RequestMapping("material")
public class CoffeeController extends BaseController {

	@Autowired
	CoffeeService coffeeService;
	@Autowired
	ManorService manorService;
	@Autowired
	TransportService transportService;
	@Autowired
	StorageService storageService;

	@GetMapping("load/infos")
	public Result loadInfos() {
		return Result.success().addData("manors", manorService.query(Cnd.where("enabled", "=", true))).addData("transports", transportService.query(Cnd.where("enabled", "=", true))).addData("storages", storageService.query(Cnd.where("enabled", "=", true)));
	}

	@GetMapping("list")
	@SINORequiresPermissions(InstallPermission.MATERIAL_LIST)
	@ApiOperation(value = "原料分页列表")
	public Result list(@RequestParam(value = "page", defaultValue = "1") @ApiParam("页码") int page) {
		PageredData<Coffee> pager = coffeeService.searchByPage(fixPage(page), Cnd.where("enabled", "=", true).desc("id"));
		Lang.each(pager.getDataList(), new Each<Coffee>() {

			@Override
			public void invoke(int index, Coffee coffee, int length) throws ExitLoop, ContinueLoop, LoopException {
				coffee.setManor(manorService.fetch(coffee.getManorId()));
				coffee.setStorage(storageService.fetch(coffee.getStorageId()));
				coffee.setTransport(transportService.fetch(coffee.getTransportId()));
			}
		});
		return Result.success().addData("pager", pager);
	}

	@GetMapping("search")
	@SINORequiresPermissions(InstallPermission.MATERIAL_LIST)
	@ApiOperation(value = "原料分页检索")
	public Result search(@RequestParam("key") @ApiParam("搜索关键字") String key, @RequestParam(value = "page", defaultValue = "1") @ApiParam("页码") int page) {
		PageredData<Coffee> pager = coffeeService.searchByKeyAndPage(
				fixSearchKey(key),
				fixPage(page),
				Cnd.where("enabled", "=", true),
				"name")
				.addParam("key", key);
		Lang.each(pager.getDataList(), new Each<Coffee>() {

			@Override
			public void invoke(int index, Coffee coffee, int length) throws ExitLoop, ContinueLoop, LoopException {
				coffee.setManor(manorService.fetch(coffee.getManorId()));
				coffee.setStorage(storageService.fetch(coffee.getStorageId()));
				coffee.setTransport(transportService.fetch(coffee.getTransportId()));
			}
		});
		return Result.success()
				.addData("pager", pager);
	}

	@PostMapping(value = "add")
	@SINORequiresPermissions(InstallPermission.MATERIAL_ADD)
	@ApiOperation(value = "新增原料")
	public Result save(@RequestBody Coffee coffee) {
		return coffeeService.save(coffee) == null ? Result.fail("保存原料信息失败!") : Result.success().addData("coffee", coffee);
	}

	@PostMapping(value = "edit")
	@SINORequiresPermissions(InstallPermission.MANOR_EDIT)
	public Result edit(@RequestBody Coffee coffee) {
		return coffeeService.update(coffee) == 1 ? Result.success() : Result.fail("更新原料信息失败!");
	}
	
	/**
	 * 删除原料
	 * 
	 * @param id
	 *            原料id
	 * @return
	 */
	@GetMapping("delete/{id}")
	@SINORequiresPermissions(value = { InstallPermission.AREA_DELETE })
	@ApiOperation("删除原料")
	public Result delete(@PathVariable("id") @ApiParam("原料id") long id) {
		return coffeeService.update(Chain.make("enabled", false), Cnd.where("id", "=", id)) == 1 ? Result.success() : Result.fail("删除原料失败!");
	}
}
